package com.leyou.item.web;

import com.leyou.common.entity.Category;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.CategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    //构造器注入
    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 根据分类id，查询商品分类
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id) {
        Category category = categoryService.getById(id);
        return ResponseEntity.ok(new CategoryDTO(category));
    }

    /**
     * 根据分类id的集合，查询商品分类的集合
     *
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> ids) {
        List<Category> list = categoryService.listByIds(ids);
        return ResponseEntity.ok(CategoryDTO.convertEntityList(list));
    }

    /**
     * 根据父类目id，查询子类目的集合
     * @param pid
     * @return
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){
        List<CategoryDTO> list =  categoryService.queryCategoryByParentId(pid);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据品牌id，查询商品分类的集合
     * @param bid
     * @return
     */
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam("id") Long bid){
        List<CategoryDTO> list = categoryService.queryCategoryByBrandId(bid);
        return ResponseEntity.ok(list);
    }
}

